function Y = vecparm_unc(X,numvars,numcomp)

if isstruct(X)

    cov_chol = cellfun(@(x) chol(x),mat2cell(X.cov,numvars,numvars,ones(1,numcomp)),'unif',0);
    cov_chol = cat(3,cov_chol{:});
    chol_diag_ind = repmat(eye(numvars)==1,[1 1 numcomp]);
    cov_chol(chol_diag_ind) = log(cov_chol(chol_diag_ind));

    pr_log = log(X.pr(1:end-1)./X.pr(end));

    Y = [X.mean(:); 
         cov_chol(repmat(triu(true(numvars)),[ 1 1 numcomp]));
         pr_log(:)];

else

    np_mean = numvars*numcomp;
    np_cov = ((numvars^2-numvars)/2 + numvars)*numcomp;
    np_pr = numcomp - 1;
    
    X = mat2cell(X,[np_mean; np_cov; np_pr]);

    cov_chol = zeros([numvars numvars numcomp]);
    cov_chol(repmat(triu(true(numvars)),[1 1 numcomp])) = X{2};
    chol_diag_ind = repmat(eye(numvars)==1,[1 1 numcomp]);
    cov_chol(chol_diag_ind) = exp(cov_chol(chol_diag_ind));
    cov_mat = cellfun(@(x) x'*x,mat2cell(cov_chol,numvars,numvars,ones(1,numcomp)),'unif',0);
    cov_mat = cellfun(@(x) (x + x')./2,cov_mat,'unif',0);

    pr_log = [X{3}' 0];

    Y = struct('mean',reshape(X{1},[numvars numcomp])...
            ,'cov',cat(3,cov_mat{:})...
            ,'pr',exp(pr_log)./sum(exp(pr_log)));
end

end




